<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>...运算符</title>
    <script>
      //    可变长参数
      //   定义函数时，可以将参数指定为可变数组
      function sum(...args) {
        let result = 0;
        args.forEach((val) => {
          result += val;
        });
        return result;
      }
      console.log(sum(1, 2, 3)); // 6
      console.log(sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); // 55
      //   复制数组
      let arr = [1, 2, 3];
      let arr2 = [...arr];
      console.log(arr2); //[1, 2, 3];
      //数组含空位
      let arr3 = [1, , 3];
      let arr4 = [...arr3];
      console.log(arr4); //[1, undefined, 3]
      //合并数组
      console.log([...[1, 2], ...[3, 4]]); // [1, 2, 3, 4]
      // 将字符串转为真正的数组。
      console.log([..."hello"]); // [ "h", "e", "l", "l", "o" ]
    </script>
  </head>
  <body></body>
</html>
